package 牛客网编程.剑指offer.从尾到头打印链表;

/**
 * @author Veng Su 1344114844@qq.com
 * @date 2018/7/14 15:39
 */

import java.util.ArrayList;
import java.util.Collections;

class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}


public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        if (listNode == null) return new ArrayList<Integer>();
        else if (listNode.next == null) {
            result.add(listNode.val);
            return result;
        }
        result.add(listNode.val);
        do {
            listNode = listNode.next;
            result.add(listNode.val);
        } while (listNode.next != null);
        Collections.reverse(result);
        return result;
    }
}
